package datacreation;


import guiobjects.Observation;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

import commonfunctions.ChooseFile;

import user.data.Data;

/**
 * This class was created in order to parse observation data from an external, supplied file.
 * Uses the document datacreation/parser to get the format for parsing.
 * 
 * @author Trevor Terris
 *
 */
public class ParsedRealDataCreator 
{
	/**
	 * Creates the file, given a directory to write the file into.
	 * 
	 * @param dirName
	 */
	public static ArrayList<Observation> createData(String dirName)
	{
		try
		{
			ArrayList<Observation> toReturn = new ArrayList<Observation>();
	    	File parseDataFile = new File("src/datacreation/parser");
	    	ArrayList<String> parse = new ArrayList<String>();
	    	Scanner parseScan = new Scanner(parseDataFile);
	    	
	    	while(parseScan.hasNext())
	    		parse.add(parseScan.next());
	    	
	    	Scanner s = ChooseFile.select("Select the file containing original observations.");
	    	String firstLine = s.nextLine();
	    	String[] firstLineSplit = firstLine.split(" ");
	    	double[] firstLineDouble = new double[firstLineSplit.length];
	    	double[] maxVals = new double[firstLineSplit.length];
	    	double[] minVals = new double[firstLineSplit.length];
	    	for(int i = 0; i < firstLineDouble.length; i++)
	    	{
	    		firstLineDouble[i] = Double.parseDouble(firstLineSplit[i]);
	    		maxVals[i] = firstLineDouble[i];
	    		minVals[i] = firstLineDouble[i];
	    	}
	    	ArrayList<double[]> inputs = new ArrayList<double[]>();
	    	inputs.add(firstLineDouble);
	    	int numRead = 0;
	    	while(s.hasNext() && numRead < Data.numToParse)
	    	{
	    		numRead++;
	    		double[] line = new double[firstLineDouble.length];
	    		for(int i = 0; i < line.length; i++)
	    		{
	    			line[i] = Double.parseDouble(s.next());
	    			maxVals[i] = (line[i] > maxVals[i])?line[i]:maxVals[i];
	    			minVals[i] = (line[i] < minVals[i])?line[i]:minVals[i];
	    		}
	    		inputs.add(line);
	    	}
	    	
	    	for(int i = 0; i < inputs.size(); i++)
	    	{
	    		int f = parse.indexOf("f");
	    		int x = parse.indexOf("x");
	    		int z = parse.indexOf("z");
	    		int c = parse.indexOf("c");
	    		double[] line = inputs.get(i);
	    		String parseString = convert(line[x], maxVals[x], minVals[x]) + " " + 
	    							(1.0 - convert(line[z], maxVals[z], minVals[z])) + " "; 
	    		for (int j = 9; j < line.length; j++)
	    		{
	    			if(j >= parse.size() || parse.get(j).equals("a"))
	    				parseString += convert(line[j], maxVals[j], minVals[j]) + " ";
	    		}
	    		parseString += "0";
	    		if (c != -1)
	    			parseString += " " + line[c];
	    		Observation thisOb = new Observation(parseString);
	    		thisOb.frameNum = (int)line[f];
	    		toReturn.add(thisOb);
	    	}
	    	return toReturn;
		}
		catch (Exception e)
		{
			System.out.println("Error.");
			e.printStackTrace();
			return new ArrayList<Observation>();
		}
	}
	private static double convert(double val, double max, double min)
	{
		return (val - min)/(max - min);
	}
}
